package com.zy.controller;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.qianfan.QianFanChatModel;
import org.springframework.ai.qianfan.QianFanChatOptions;
import org.springframework.ai.qianfan.api.QianFanApi;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RestController
public class AiController {
    private final QianFanChatModel chatClient;
    private final ChatMemory chatMemory;
    public AiController(QianFanChatModel chatClient, ChatMemory chatMemory) {
        this.chatClient = chatClient;
        this.chatMemory = chatMemory;
    }
    @PostMapping(value = "/ai")
    public String generation(@RequestBody String message) {
        //创建消息列表
        List<Message> messages = new ArrayList<>();
        //添加用户信息
        messages.add(new UserMessage(message));
        //添加系统信息
        messages.add(new SystemMessage("你是一个用户管理客服，专门管理用户的增删改查功能，只有当用户向你提出增删改查功能性的要求是，你要要求客户出示姓名，并且你要对他进行确认。你要表现出"));
        //添加对话模板
        var prompt = new Prompt(messages);

        PromptChatMemoryAdvisor promptChatMemoryAdvisor = new PromptChatMemoryAdvisor(chatMemory);
        //返回结果
        ChatResponse response = this.chatClient.call(prompt);
        return response.getResult().getOutput().getContent();
    }
}
